package com.xzmzhp.service.power.impl;

import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.dao.power.CompetenceMapper;
import com.xzmzhp.dao.power.RequestPathMapper;
import com.xzmzhp.pojo.power.Competence;
import com.xzmzhp.pojo.power.RequestPath;
import com.xzmzhp.service.power.CompetenceService;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王源(xm)
 * @Date: 2020/11/15 18:46
 * @Description:
 */
@Service
public class CompetionServiceImpl implements CompetenceService {

    @Autowired
    private CompetenceMapper competenceMapper;

    @Autowired
    private RequestPathMapper requestPathMapper;

    /**
     * 查询权限信息表所有数据
     *
     * @return ReturnData对象 成功返回 data 失败返回错误信息和错误状态码
     */
    @Override
    public ReturnData findAll() {
        List<Competence> list = competenceMapper.findAll();
        if (list != null) {
            return ReturnData.ok(list);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "没有找到权限信息");
        }
    }

    /**
     * 根据id查询权限名称
     *
     * @param competenceId 权限信息表id
     * @return ReturnData对象 成功返回 data 失败返回错误信息和错误状态码
     */
    @Override
    public ReturnData findById(int competenceId) {
        Competence competence = competenceMapper.findById(competenceId);
        if (competence != null) {
            return ReturnData.ok(competence);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "没有找到对应的权限信息");
        }
    }

    /**
     * 根据权限名称查询数据
     *
     * @param competenceName 权限名称
     * @return
     */
    @Override
    public ReturnData findByName(String competenceName) {
        Competence competence = competenceMapper.findByName(competenceName);
        if (competence != null) {
            List<Competence> list = new ArrayList<>();
            list.add(competence);
            return ReturnData.ok(list);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "没有找到对应的权限信息");
        }
    }

    /**
     * 根据权限id查询所有路径
     *
     * @param competenceId 权限id
     * @return
     */
    @Override
    public ReturnData findByCompetenceId(int competenceId) {
        if (competenceId > 0) {
            List<RequestPath> list = competenceMapper.findByCompetenceId(competenceId);
            if (list != null) {
                return ReturnData.ok(list);
            }
            return ReturnData.error(ReturnConstant.FAIL, "查询信息失败");
        }
        return ReturnData.error(ReturnConstant.FAIL, "权限id不正确");
    }

    /**
     * 添加权限
     *
     * @param competenceName 权限名称
     * @return ReturnData对象 成功返回 data 失败返回错误信息和错误状态码
     */
    @Override
    public ReturnData addCompetence(String competenceName) {
        Integer i = competenceMapper.addCompetence(competenceName);
        if (i > 0) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "未知原因影响了数据库，添加失败了");
        }
    }

    /**
     * 修改权限
     *
     * @param competence 对象
     * @return ReturnData对象 成功返回 data 失败返回错误信息和错误状态码
     */
    @Override
    public ReturnData updateCompetence(Competence competence) {
        if (competence != null) {
            Integer i = competenceMapper.updateCompetence(competence);
            if (i > 0) {
                return ReturnData.ok();
            } else {
                return ReturnData.error(ReturnConstant.FAIL, "未知原因影响了数据库，修改失败了");
            }
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "未知原因影响了数据传输没有获取到权限信息");
        }
    }

    /**
     * 删除权限
     *
     * @param competenceId 权限id
     * @return ReturnData对象 成功返回 data 失败返回错误信息和错误状态码
     */
    @Override
    public ReturnData deleteCompetence(int competenceId) {
        Integer i = competenceMapper.deleteCompetence(competenceId);
        if (i > 0) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "未知原因影响了数据库，删除失败了");
        }
    }
}
